package hxy.bytecode.base;

import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

public class Imagebase64 {

    public static void main(String[] args) {
//        System.out.println(getImageBinary()); // image to base64
//        base64StringToImage(getImageBinary()); // base64 to image
        String code = "iVBORw0KGgoAAAANSUhEUgAAAPoAAAD6CAYAAACI7Fo9AAAVAElEQVR4Xu2TQY4ky47E3v0vPbNuAgbCBWW46rsI2KZBWaiV5f/937Is//P8x39YluV/j33oy/IA+9CX5QH2oS/LA+xDX5YH2Ie+LA+wD31ZHmAf+rI8wD70ZXmAfejL8gD70JflAfahL8sD7ENflgfYh74sD7APfVkeYB/6sjzAPvRleYB96MvyAPvQl+UB9qEvywPsQ1+WB9iHviwPsA99WR5gH/qyPMA+9GV5gH3oy/IA+9CX5QH2oS/LA+xDX5YH2Ie+LA+wD31ZHmAf+rI8wD70ZXmAfejL8gD70JflAfahL8sD7ENflgcY+9D/+++/P5EEve4k6FWToGd+F/ze1Exl7GY84NQk6HUnQa+aBD3zu+D3pmYqYzfjAacmQa87CXrVJOiZ3wW/NzVTGbsZDzg1CXrdSdCrJkHP/C74vamZytjNeMCpSdDrToJeNQl65nfB703NVMZuxgNOTYJedxL0qknQM78Lfm9qpjJ2Mx7w9iG5h+1Dz/wE56s9CfZaPz3zE5y3Hnrm/xrucXsfY+xmPODtQ3IP24ee+QnOV3sS7LV+euYnOG899Mz/Ndzj9j7G2M14wNuH5B62Dz3zE5yv9iTYa/30zE9w3nromf9ruMftfYyxm/GAtw/JPWwfeuYnOF/tSbDX+umZn+C89dAz/9dwj9v7GGM34wFvH5J72D70zE9wvtqTYK/10zM/wXnroWf+r+Eet/cxxm7GA94+JPewfeiZn+B8tSfBXuunZ36C89ZDz/xfwz1u72OM3YwHtEPSqyZBz/wE56s9CfZ+1X+aUzhvPfSqSdAz/zZjN+MB7ZD0qknQMz/B+WpPg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mx7QDkmvmgS9qp9yCudvJ0Gv209wvrsnZSpjN+MB7ZD0qknQq/opp3D+dhL0uv0E57t7UqYydjMe0A5Jr5oEvaqfcgrnbydBr9tPcL67J2UqYzfjAe2Q9KpJ0Kv6Kadw/nYS9Lr9BOe7e1KmMnYzHtAOSa+aBL2qn3IK528nQa/bT3C+uydlKmM34wHtkPSqSdCr+imncP52EvS6/QTnu3tSpjJ2Mx7w9iG5h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QPd/qG4h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QPd/qG4h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QPd/qG4h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QPd/qG4h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QPd/qG4h+1Dr+qfJkGvmi7YW+3nvOVVxv7P+QNNTYJet5/gvCVBz3IK5y1dsLfaz/lqz1eM3YwHnJoEvW4/wXlLgp7lFM5bumBvtZ/z1Z6vGLsZDzg1CXrdfoLzlgQ9yymct3TB3mo/56s9XzF2Mx5wahL0uv0E5y0JepZTOG/pgr3Vfs5Xe75i7GY84NQk6HX7Cc5bEvQsp3De0gV7q/2cr/Z8xdjNeMCpSdDr9hOctyToWU7hvKUL9lb7OV/t+Yq5my3L0sY+9GV5gH3oy/IA+9CX5QH2oS/LA+xDX5YH2Ie+LA+wD31ZHmAf+rI8wD70ZXmAfejL8gD70JflAfahL8sD7ENflgfYh74sD7APfVkeYB/6sjzAPvRleYB96MvyAPvQl+UB9qEvywPsQ1+WB9iHviwPsA99WR5gH/qyPMA+9GV5gH3oy/IA+9CX5QH2oS/LA+xDX5YH2Ie+LA+wD31ZHmAf+rI8wD70ZXmAfejL8gD70JflAfahL8sD7ENflgf4f/j/MAY7x5nUAAAAAElFTkSuQmCC";
        base64StringToImage(code);
    }

    static String getImageBinary() {
        File f = new File("/home/eric/Pictures");
        try {
            BufferedImage bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

            return Base64.encodeBase64String(bytes);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    static void base64StringToImage(String base64String) {
        try {

            byte[] bytes = Base64.decodeBase64(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
            BufferedImage bi1 = ImageIO.read(bais);
            File f1 = new File("out.png");
            System.out.println(f1.getAbsolutePath());
            ImageIO.write(bi1, "png", f1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}